﻿using BackData;
using Domain;
using Domain.DTOs.排程;
using Domain.Quality;
using Domain.工艺工序;
using Domain.生产订单;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.排程;
using Team.API.Services;

namespace Team.API.Applocation.Hanldler.排程
{
    /// <summary>
    /// 处理获取生产计划命令的处理器
    /// </summary>
    public class GetProductionCommandHandler : IRequestHandler<GetProductionCommand, APIPaging<ProductionDtos>>
    {
        private readonly IRepository<Productionschedulemodel> _productRepository;
        private readonly IRepository<OrderModel> _orderRepository;
        private readonly IRepository<Makestepmodel> _makeStepRepository;
        private readonly IRepository<Equipmentmodel> _equipmentRepository;
        private readonly IEquipmentStatusService _equipmentStatusService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productRepository">生产计划仓储</param>
        /// <param name="orderRepository">订单仓储</param>
        /// <param name="makeStepRepository">工序仓储</param>
        /// <param name="equipmentRepository">设备仓储</param>
        /// <param name="equipmentStatusService">设备状态服务</param>
        public GetProductionCommandHandler(
            IRepository<Productionschedulemodel> productRepository,
            IRepository<OrderModel> orderRepository,
            IRepository<Makestepmodel> makeStepRepository,
            IRepository<Equipmentmodel> equipmentRepository,
            IEquipmentStatusService equipmentStatusService)
        {
            _productRepository = productRepository;
            _orderRepository = orderRepository;
            _makeStepRepository = makeStepRepository;
            _equipmentRepository = equipmentRepository;
            _equipmentStatusService = equipmentStatusService;
        }

        /// <summary>
        /// 处理获取生产计划请求
        /// </summary>
        /// <param name="request">获取生产计划命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>生产计划分页结果</returns>
        public async Task<APIPaging<ProductionDtos>> Handle(GetProductionCommand request, CancellationToken cancellationToken)
        {
            // 检查并更新设备状态（使用共享服务）
            await _equipmentStatusService.CheckAndUpdateEquipmentStatusAsync("查询触发", cancellationToken);

            // 获取未删除的数据
            var productionSchedules = _productRepository.GetAll().Where(x => !x.IsDel).ToList();
            var orders = _orderRepository.GetAll().Where(x => !x.IsDel).ToList();
            var makeSteps = _makeStepRepository.GetAll().Where(x => !x.IsDel).ToList();
            var equipments = _equipmentRepository.GetAll().Where(x => !x.IsDel).ToList();

            // LINQ 查询连接数据
            var query = from schedule in productionSchedules
                        join order in orders on schedule.OrderId equals order.Id
                        join makeStep in makeSteps on schedule.Makestepid equals makeStep.Id
                        join equipment in equipments on makeStep.EquipmentId equals equipment.Id
                        select new ProductionDtos
                        {
                            Id = schedule.Id,
                            Plannumber = schedule.Plannumber,
                            Processname = makeStep.Processname,
                            Kahuna = makeStep.Kahuna,
                            Goodsname = makeStep.Goodsname,
                            Goodtime = order.Ordertime,
                            Planstate = schedule.Planstate,
                            Planstatetime = schedule.Planstatetime,
                            Planendtime = schedule.Planendtime,
                            Statetime = schedule.Statetime,
                            Endtime = schedule.Endtime,
                            Equipmentname = equipment.Equipmentname,
                            Manager = equipment.Manager,
                            OrderNumber = order.Number,
                        };

            var result = query.OrderBy(x => x.Statetime).ToList();

            return new APIPaging<ProductionDtos>()
            {
                Code = StateCode.OK,
                Message = "显示计划成功",
                Totalcount = result.Count,
                ListResult = result
            };
        }


    }
}
